﻿using Microsoft.SharePoint.WebControls;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SZF.WebParts.QuadBanner
{
    [ToolboxItemAttribute(false)]
    public partial class QuadBanner : WebPart
    {
        public QuadBanner()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        

        protected void Page_Load(object sender, EventArgs e)
        { 
            //Apply width and height to the banner
            String CssLiteral = String.Format("<style> #{0} .BnrContent .slider li {{width: {1}px; height: {2}px;}}</style>", 
                                                this.ClientID, 
                                                BannerWidth, 
                                                BannerHeight);

            this.Controls.Add(new Literal { Text = CssLiteral });

            this.Page.ClientScript.RegisterClientScriptInclude("easySlider1.7.js", "/_Layouts/SZF.WebParts/Scripts/easySlider1.7.js");
            this.Page.ClientScript.RegisterClientScriptInclude("SliderHelper.js", "/_Layouts/SZF.WebParts/Scripts/SliderHelper.js");

            string SliderScript =  GetSliderScript(Speed, Pause_1, Vertical_1, SPLibDisplayName, SPFolderName_1, "#slider1");
                   SliderScript += GetSliderScript(Speed, Pause_2, Vertical_2, SPLibDisplayName, SPFolderName_2, "#slider2");
                   SliderScript += GetSliderScript(Speed, Pause_3, Vertical_3, SPLibDisplayName, SPFolderName_3, "#slider3");
                   SliderScript += GetSliderScript(Speed, Pause_4, Vertical_4, SPLibDisplayName, SPFolderName_4, "#slider4");

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID, SliderScript, true);
        }

        private string GetSliderScript(int speed, int pause, bool vertical, string spLibdisplayName, string spFolderName, string sliderID)
        {
            string SliderScript = string.Empty;
            string SliderSettings =
                String.Format("{{ auto: true, speed: {0}, pause: {1}, controlsShow: false, continuous: true, vertical: {2} }}",
            speed,
            pause,
            vertical.ToString().ToLower());

            SliderScript = String.Format(@" 
                         $(document).ready(function () {{
                            getBannerImages(null, '{0}', '{1}', '{2}', '{3}', '{4}', '#{5} {6}', {7} );
                         }});
                        ", SPLibDisplayName, spFolderName, ShowColumnName, UrlColumnName, CommentsColumnName, this.ClientID, sliderID, SliderSettings);
            return SliderScript;
        }
        #region Properties
        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Relative Web URL"),
        WebDescription("Relative Web URL, that contains a library with images")]
        public string SpWebDisplayName { get { return _SpWebDisplayName; } set { _SpWebDisplayName = value; } }
        private string _SpWebDisplayName = "/";

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Library Name (without spaces!)"),
        WebDescription("")]
        public string SPLibDisplayName { get { return _SPLibDisplayName; } set { _SPLibDisplayName = value; } }
        private string _SPLibDisplayName = "ImageBanners";

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Name of the URL Column"),
        WebDescription("Column that stores the target url that will be open when clicking the picture")]
        public string UrlColumnName { get { return _UrlColumnName; } set { _UrlColumnName = value; } }
        private string _UrlColumnName = "URL";

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Column name dedicated for Comments"),
       WebDescription("Column name dedicated for Comments (does not work yet)")]
        public string CommentsColumnName { get { return _CommentsColumnName; } set { _CommentsColumnName = value; } }
        private string _CommentsColumnName = "Comments";


        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Coulm name used as a flag"),
       WebDescription("Boolean field. True = show in slider web part. No - do not show")]
        public string ShowColumnName { get { return _ShowColumnName; } set { _ShowColumnName = value; } }
        private string _ShowColumnName = "Show";

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Transition time in milliseconds"),
       WebDescription("Number in milliseconds")]
        public int Speed { get { return _Speed; } set { _Speed = value; } }
        private int _Speed = 200;

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Banner height"),
       WebDescription("Height in pixels")]
        public int BannerHeight { get { return _BannerHeight; } set { _BannerHeight = value; } }
        private int _BannerHeight = 49;

        [WebBrowsable(true),
        Category("General Banner Settings"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Banner width"),
       WebDescription("Width in pixels")]
        public int BannerWidth { get { return _BannerWidth; } set { _BannerWidth = value; } }
        private int _BannerWidth = 49;

        // ПЕРВЫЙ БАННЕР
        [WebBrowsable(true),
        Category("First Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
        public string SPFolderName_1 { get { return _SPFolderName_1; } set { _SPFolderName_1 = value; } }
        private string _SPFolderName_1 = "TopLeft";

        [WebBrowsable(true),
        Category("First Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Pause in milliseconds"),
       WebDescription("How many milliseconds it will be shows between transitions")]
        public int Pause_1 { get { return _Pause_1; } set { _Pause_1 = value; } }
        private int _Pause_1 = 2000;

        [WebBrowsable(true),
        Category("First Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Vertical slider"),
       WebDescription("Yes - images slide vertically. No - horizontally")]
        public bool Vertical_1 { get { return _Vertical_1; } set { _Vertical_1 = value; } }
        private bool _Vertical_1 = true;

        // Second Banner
        [WebBrowsable(true),
        Category("Second Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
        public string SPFolderName_2 { get { return _SPFolderName_2; } set { _SPFolderName_2 = value; } }
        private string _SPFolderName_2 = "TopRight";

        [WebBrowsable(true),
        Category("Third Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Pause in milliseconds"),
       WebDescription("How many milliseconds it will be shows between transitions")]
        public int Pause_2 { get { return _Pause_2; } set { _Pause_2 = value; } }
        private int _Pause_2 = 2000;

        [WebBrowsable(true),
        Category("Third Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Vertical slider"),
       WebDescription("Yes - images slide vertically. No - horizontally")]
        public bool Vertical_2 { get { return _Vertical_2; } set { _Vertical_2 = value; } }
        private bool _Vertical_2 = true;

        // Third Banner
        [WebBrowsable(true),
        Category("Third Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
        public string SPFolderName_3 { get { return _SPFolderName_3; } set { _SPFolderName_3 = value; } }
        private string _SPFolderName_3 = "BottomLeft";

        [WebBrowsable(true),
        Category("Third Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Pause in milliseconds"),
       WebDescription("How many milliseconds it will be shows between transitions")]
        public int Pause_3 { get { return _Pause_3; } set { _Pause_3 = value; } }
        private int _Pause_3 = 2000;

        [WebBrowsable(true),
        Category("Third Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
        public bool Vertical_3 { get { return _Vertical_3; } set { _Vertical_3 = value; } }
        private bool _Vertical_3 = true;

        // Fourth Banner
        [WebBrowsable(true),
        Category("Fourth Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Folder Name"),
       WebDescription("Folder Name, that holds images in the library")]
        public string SPFolderName_4 { get { return _SPFolderName_4; } set { _SPFolderName_4 = value; } }
        private string _SPFolderName_4 = "BottomRight";

        [WebBrowsable(true),
        Category("Fourth Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Pause in milliseconds"),
       WebDescription("How many milliseconds it will be shows between transitions")]
        public int Pause_4 { get { return _Pause_4; } set { _Pause_4 = value; } }
        private int _Pause_4 = 2000;

        [WebBrowsable(true),
        Category("Fourth Banner"),
        Personalizable(PersonalizationScope.Shared),
       WebDisplayName("Vertical slider"),
       WebDescription("Yes - images slide vertically. No - horizontally")]
        public bool Vertical_4 { get { return _Vertical_4; } set { _Vertical_4 = value; } }
        private bool _Vertical_4 = true;


        #endregion


    }
}
